package com.utuan.log;

import java.text.MessageFormat;

public class Log implements org.apache.commons.logging.Log {

	public static final int DEBUG = 0;
	public static final int INFO = 1;
	public static final int WARN = 2;
	public static final int ERROR = 3;
	public static final int FATAL = 4;
	public static final int TRACE = 5;

	private static final String ENTER_MSG_FORMAT = "enter {0}";
	private static final String ENTER_MSG_FORMAT2 = "enter {0} with parameters:{1}";

	private static final String EXIT_MSG_FORMAT = "exit {0}";
	private static final String EXIT_MSG_FORMAT2 = "exit {0} with parameters:{1}";

	private org.apache.commons.logging.Log log;
	private String fullClassName;

	public Log(org.apache.commons.logging.Log log, String className) {
		this.log = log;
		this.fullClassName = className;
	}

	public void debug(Object arg0) {
		log.debug(fullClassName + "::" + arg0);

	}

	public void debug(Object arg0, Throwable arg1) {
		log.debug(fullClassName + "::" + arg0, arg1);
	}

	public void error(Object arg0) {
		log.error(fullClassName + "::" + arg0);
	}

	public void error(Object arg0, Throwable arg1) {
		log.error(fullClassName + "::" + arg0, arg1);
	}

	public void fatal(Object arg0) {
		log.fatal(fullClassName + "::" + arg0);
	}

	public void fatal(Object arg0, Throwable arg1) {
		log.fatal(fullClassName + "::" + arg0, arg1);
	}

	public void info(Object arg0) {
		log.info(fullClassName + "::" + arg0);
	}

	public void info(Object arg0, Throwable arg1) {
		log.info(fullClassName + "::" + arg0, arg1);
	}

	public boolean isDebugEnabled() {
		return log.isDebugEnabled();
	}

	public boolean isErrorEnabled() {
		return log.isErrorEnabled();
	}

	public boolean isFatalEnabled() {
		return log.isFatalEnabled();
	}

	public boolean isInfoEnabled() {
		return log.isInfoEnabled();
	}

	public boolean isTraceEnabled() {
		return log.isTraceEnabled();
	}

	public boolean isWarnEnabled() {
		return log.isWarnEnabled();
	}

	public void trace(Object arg0) {
		log.trace(fullClassName + "::" + arg0);
	}

	public void trace(Object arg0, Throwable arg1) {
		log.trace(fullClassName + "::" + arg0, arg1);
	}

	public void warn(Object arg0) {
		log.warn(fullClassName + "::" + arg0);
	}

	public void warn(Object arg0, Throwable arg1) {
		log.warn(fullClassName + "::" + arg0, arg1);
	}

	public void enter(String methodName, Object... params) {
		enter(DEBUG, methodName, params);
	}

	public void enter(int logLevel, String methodName, Object... params) {
		String msg = null;
		if (params == null || params.length == 0) {
			msg = MessageFormat.format(ENTER_MSG_FORMAT, methodName);
		} else {
			msg = MessageFormat.format(ENTER_MSG_FORMAT2, methodName, params);
		}
		switch (logLevel) {
		case DEBUG:
			debug(msg);
			break;
		case INFO:
			info(msg);
			break;
		case WARN:
			warn(msg);
			break;
		case ERROR:
			error(msg);
			break;
		case FATAL:
			fatal(msg);
			break;
		case TRACE:
			trace(msg);
			break;
		default:
			debug(msg);
			break;
		}
	}

	public void exit(String methodName, Object... params) {
		exit(DEBUG, methodName, params);
	}

	public void exit(int logLevel, String methodName, Object... params) {
		String msg = null;
		if (params == null || params.length == 0) {
			msg = MessageFormat.format(EXIT_MSG_FORMAT, methodName);
		} else {
			msg = MessageFormat.format(EXIT_MSG_FORMAT2, methodName, params);
		}
		switch (logLevel) {
		case DEBUG:
			debug(msg);
			break;
		case INFO:
			info(msg);
			break;
		case WARN:
			warn(msg);
			break;
		case ERROR:
			error(msg);
			break;
		case FATAL:
			fatal(msg);
			break;
		case TRACE:
			trace(msg);
			break;
		default:
			debug(msg);
			break;
		}
	}

}
